﻿using System;
using eCommerce.BusinessLogic;
using eCommerce.DataAccess;

namespace eCommerce.Site
{
    public partial class Product : System.Web.UI.Page
    {
        private int _item;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Int32.TryParse(Request.QueryString["t"], out _item))
            {
                pnlCategory.Visible = true;
                pnlProduct.Visible = false;
                repeaterCategory.DataSource = OrderBL.GetProductByCategory(_item);
                repeaterCategory.DataBind();
            }
            if (Int32.TryParse(Request.QueryString["i"], out _item))
            {
                pnlCategory.Visible = false;
                pnlProduct.Visible = true;
                var products = OrderBL.GetProduct(_item);
                litProduct.Text = "<h2>" + products.ProductName + "</h2><img class=\"productpic\" alt=\"" + products.ProductName + "\" src=\"product/" + products.ProductImage + "\">";
                litProductDesc.Text = "<h4>Description:</h4><p>" + products.ProductDesc + "</p><h4>Price:</h4><h3>" + Convert.ToDecimal(products.UnitPrice) + "</h3>";
            }
        }
        protected void imgbuttonAdd_Click(object sender, EventArgs e)
        {
                if (Int32.TryParse(Request.QueryString["i"], out _item))
                {
                    CartDA cartDA = new CartDA();
                    string scartid = cartDA.CartID.ToString();
                    Guid cartID = new Guid(scartid);

                    bool cartProduct = OrderBL.CheckProductCart(cartID, _item);
                    if (cartProduct)
                    {
                        var productinfo = OrderBL.GetProductCart(cartID, _item);
                        OrderBL.UpdateCartProduct(
                            productinfo, 
                            cartID,
                            _item, OrderBL.GetProductCartQty(cartID, _item) + Convert.ToInt32(ddlQTY.SelectedValue), 
                            OrderBL.GetProductType(_item), 
                            Convert.ToDateTime(DateTime.Now.ToShortDateString()), 
                            OrderBL.GetProductPrice(_item) + OrderBL.GetProductPrice(_item) * Convert.ToInt32(ddlQTY.SelectedValue));
                    }
                    else
                    {
                        OrderBL.InsertCartProduct(cartID, _item, Convert.ToInt32(ddlQTY.SelectedValue), OrderBL.GetProductType(_item), Convert.ToDateTime(DateTime.Now.ToShortDateString()), OrderBL.GetProductPrice(_item) * Convert.ToInt32(ddlQTY.SelectedValue));
                       
                    }
                }
            
            Response.Redirect("Basket.aspx");
        }
    }
}
